package com.ys.controller.api.business;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ys.config.MenuId;
import com.ys.entity.business.TpProductActive;
import com.ys.entity.business.TpProductActiveComponent;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.entity.sys.YsCol;
import com.ys.service.business.TpProductActiveService;
import com.ys.service.business.TpProductBasicComponentService;
import com.ys.service.business.YsbExportLogService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.PdfService;
import com.ys.service.sys.PdfService.ListingSubDto;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Listing库子件
 */
@Controller
@RequestMapping("/api/inter/tpProductActiveComponent")
public class ApiInterTpProductActiveComponentController {
	private static final Logger logger = LoggerFactory.getLogger(ApiInterTpProductActiveComponentController.class);

	@Autowired
	private CommonApiService<TpProductActiveComponent> commonApiService;


	/**
	 * 设置查询的cond的sql设置
	 * @param cond
	 * @return
	 */
	public TpProductActiveComponent setSeachSql(TpProductActiveComponent cond){
		cond.addLeftJoin("TP_ProductActive B ON B.code = A.pcode")
				.addField("B.Name pName");
		return  cond;
	}

	/**
	 * 查询(1688铺货调用)
	 */
	@RequestMapping("/searchPh")
	@ResponseBody
	public LayuiTableVoJA searchPh(Model model, HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute TpProductActiveComponent cond){
		try {
			cond.where("(SELECT count(1) FROM TCG_Distribution B WHERE B.Scode = A.code) = 0");
			cond = setSeachSql(cond);

			LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);
			return tableVoJA;
		}catch (Exception e){
			logger.error("查询Listing库出错：",e.getMessage(),e);
			LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
			tableVoJA.setCode(1);
			tableVoJA.setMsg("查询出错，请稍后重试！");
			return tableVoJA;
		}
	}



	/**
	 * 弹出框（根据母件编码调取子件信息）
	 */
	@RequestMapping("/searchByPcode")
	@ResponseBody
	public LayuiTableVoJA searchByPcode(Model model, HttpServletRequest request, HttpServletResponse response,
								 @ModelAttribute TpProductActiveComponent cond, String conditions){
		try {
			String _ys_pCode = ConditionUtil.getConditionValue(conditions, "_ys_pCode");//母件编码
			if (_ys_pCode != null) {
				cond.where("A.pcode ='" + _ys_pCode + "'");
			}

			cond = setSeachSql(cond);
			cond.addField("A.pcode AS  _ys_pCode");

			LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);
			return tableVoJA;
		}catch (Exception e){
			logger.error("查询Listing库出错：",e.getMessage(),e);
			LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
			tableVoJA.setCode(1);
			tableVoJA.setMsg("查询出错，请稍后重试！");
			return tableVoJA;
		}
	}



}